﻿/*
Copyright (C) 2011 by Matthew Hickson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.Collections;

namespace thinkschematwo.IO
{
    class GroupedListParser
    {

        private const String DEFAULT_GROUP_ITEM_MARKER = "+";
        private const String DEFAULT_DATA_ITEM_MARKER = "";
        private const String DEFAULT_COMMENT_MARKER = "#";
        private String DEFAULT_LINE_DELIMITER = Environment.NewLine; // NOTE: const doesn't like NewLine...

        private thinkschematwo.Collections.GroupedList list;

        private String groupItemMarker;
        private String dataItemMarker;
        private String commentMarker;
        private String lineDelimiter;

        public String GroupItemMarker {
            get { return groupItemMarker; }
            set { groupItemMarker = value; }
        } // GroupItemMarker

        public String DataItemMarker {
            get { return dataItemMarker; }
            set { dataItemMarker = value; }
        } // DataItemMarker

        public String CommentMarker {
            get { return commentMarker; }
            set { commentMarker = value; }
        } // CommentMarker

        public String LineDelimiter {
            get { return lineDelimiter; }
            set { lineDelimiter = value; }
        }

        public GroupedListParser()
        {
            GroupItemMarker = DEFAULT_GROUP_ITEM_MARKER;
            DataItemMarker = DEFAULT_DATA_ITEM_MARKER;
            CommentMarker = DEFAULT_COMMENT_MARKER;
            LineDelimiter = DEFAULT_LINE_DELIMITER;
        } // GroupedListParser

        public thinkschematwo.Collections.GroupedList ParseList(String groupedListAsString)
        {
            if (!String.IsNullOrEmpty(groupedListAsString))
            {
                list = new thinkschematwo.Collections.GroupedList();

                String currentGroup = "";
                String[] lines = groupedListAsString.Split(lineDelimiter.ToCharArray());

                foreach (String line in lines)
                {
                    if (String.IsNullOrEmpty(line))
                    {
                        // PASS: ...
                    }
                    else
                    {
                        String marker = line[0].ToString();

                        if (marker == CommentMarker)
                        {
                            // PASS: We don't do anything with comments... (should preserve them I suppose...)
                        }
                        else if (marker == GroupItemMarker)
                        {
                            currentGroup = line.Substring(1, line.Length - 1);
                            list.AddGroup(currentGroup);
                        }
                        else if (marker == DataItemMarker)
                        {
                            list.AddDataItem(line.Substring(1, line.Length - 1), currentGroup);
                        }
                        else
                        {
                            if (String.IsNullOrEmpty(DataItemMarker))
                            {
                                // NOTE: Counts as data...
                                list.AddDataItem(line, currentGroup);
                            }
                            else
                            {
                                // PASS: ...
                            }
                        }
                    }
                }

                return list;
            }

            return null;
        } // ParseList

    }
}
